package com.sdy.kzyy.web.controller;

import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.kzyy.biz.dto.OrderRecordDTO;
import com.sdy.kzyy.biz.dto.RecordInfoDTO;
import com.sdy.kzyy.biz.model.User;
import com.sdy.kzyy.biz.service.OrderRecordService;
import com.sdy.common.model.Response;
import com.sdy.kzyy.biz.service.UserService;
import io.swagger.annotations.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sdy.mvc.controller.BaseController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zzq
 * @since 2020-03-03
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@Api(tags = "预约订单API")
@RequestMapping("/orderRecord")
public class OrderRecordController extends BaseController {
    @Autowired
    private OrderRecordService orderRecordService;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "根据id查询出预约订单详情信息", response = RecordInfoDTO.class)
    @ApiImplicitParam(name = "id", value = "id")
    @GetMapping("/getRecordInfoById")
    public Response getRecordInfoById(Integer id) throws BizException {
        RecordInfoDTO recordInfoDTO = orderRecordService.getRecordInfoById(id);
        return Response.success(recordInfoDTO);
    }

    /**
     * 个人预约记录
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "个人预约记录",response = OrderRecordDTO.class)
    @GetMapping("/getSelfOrderRecord")
    public Response<List<OrderRecordDTO>> getSelfOrderRecord(HttpServletRequest request) throws BizException {
        Integer userId = getUserId(request);
        List<OrderRecordDTO> selfOrderAll = orderRecordService.getSelfOrderAll(userId);
        return Response.success(selfOrderAll);
    }

    /**
     * 扫描二维码获取预约信息
     *
     * @param token
     * @return
     */
    @GetMapping("/getOrderRecordInfo")
    @ApiOperation(value = "扫描二维码获取预约信息")
    @ApiImplicitParam(value = "token", name = "token")
    public Response getOrderRecordInfo(HttpServletRequest request, String token) throws BizException {
        Integer userId = getUserId(request);
        User user = userService.getById(userId);
        if(null == user.getQrCheck() || user.getQrCheck() != 1) {
            return Response.error("无扫码权限");
        }
        return Response.success(orderRecordService.getOrderRecordInfo(token));
    }

    /**
     * 确认口罩领取
     */
    @PostMapping("confirmMasksGot")
    @ApiOperation(value = "确认口罩领取")
    public Response confirmMasksGot(@RequestBody @ApiParam(value = "orderId",name = "订单id") Map<String,String> map,
                                    HttpServletRequest request) throws BizException {
        Integer userId = getUserId(request);
        String orderId = map.get("orderId");
        Assert.isNull(orderId, "无法获取订单信息");
        Boolean update = orderRecordService.confirmMasksGot(orderId, userId);
        return update ? Response.success("领取成功") : Response.error("更改订单状态失败");
    }

}
